package com.course.server.service.impl;

import com.course.server.domain.Chapter;
import com.course.server.domain.ChapterExample;
import com.course.server.dto.ChapterDto;
import com.course.server.dto.ChapterPageDto;
import com.course.server.dto.PageDto;
import com.course.server.exception.ValidatorException;
import com.course.server.mapper.ChapterMapper;
import com.course.server.service.ChapterService;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import com.course.server.util.ValidatorUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Classname ChapterServiceImpl
 * @Description TODO
 * @Date 2023-11-04 22:13
 * @Created by 贾悦栋
 */
@Service
public class ChapterServiceImpl implements ChapterService {

    @Resource
    private ChapterMapper chapterMapper;

    /**
     * 查询所有章节的列表
     * @return
     */
    @Override
    public void getList(ChapterPageDto chapterPageDto) {
        //分页实现
        PageHelper.startPage(chapterPageDto.getPage(),chapterPageDto.getSize());
        ChapterExample chapterExample = new ChapterExample();
        //chapterExample.createCriteria();只能创建一次
        ChapterExample.Criteria criteria = chapterExample.createCriteria();
        if (StringUtils.hasLength(chapterPageDto.getCourseId())){
            criteria.andCourseIdEqualTo(chapterPageDto.getCourseId());
        }
        //空的构造相当于什么参数都没有
        List<Chapter> chapterList = chapterMapper.selectByExample(chapterExample);
        PageInfo<Chapter> chapterPageInfo = new PageInfo<>(chapterList);
        chapterPageDto.setTotal(chapterPageInfo.getTotal());
        //实体类返回Dto类，来实现隐藏部分功能，用于前端交互 数据传输对象（Data Transfer Object）
        List<ChapterDto> chapterDtoList = CopyUtil.copyList(chapterList, ChapterDto.class);
        // List<ChapterDto> chapterDtoList = new ArrayList<>();
        // for (Chapter chapter : chapterList) {
        //     ChapterDto chapterDto = new ChapterDto();
        //     BeanUtils.copyProperties(chapter, chapterDto);
        //     chapterDtoList.add(chapterDto);
        // }

        chapterPageDto.setList(chapterDtoList);
    }

    /**
     * 保存章节信息 可以对应新增和修改
     * @param chapterDto
     */
    @Override
    public void save(ChapterDto chapterDto) {
        //使用封装好的CopyUtil进行赋值
        Chapter chapter = CopyUtil.copy(chapterDto, Chapter.class);
        //hasLength(String)对空格有效返回true、hasText(String)对空格无效返回false
        if (StringUtils.hasLength(chapter.getId())){
            boolean update = this.update(chapter);
        }else {
            boolean insert = this.insert(chapter);
        }
    }

    /**
     * 根据id删除大章信息
     * @param chapterId
     */
    @Override
    public void deleteByChapterId(String chapterId) {
        if (!chapterId.isEmpty()){
            chapterMapper.deleteByPrimaryKey(chapterId);
        }
    }

    private boolean insert(Chapter chapter){
        //chapterDto只有章节名称和课程id，需要自动生成章节id
        chapter.setId(UuidUtil.getShortUuid());
        int count = chapterMapper.insert(chapter);
        return count != 0;
    }

    private boolean update(Chapter chapter){
        int count = chapterMapper.updateByPrimaryKey(chapter);
        return count != 0;
    }
}
